// Drummer Server
// by Andrea Bianchi
// drummer.alsoplantsfly.com

/*
Copyright (C) 2009, Andrea Bianchi

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

import promidi.*;
import java.util.HashMap;

class DrummerMidi{

  public final int TOT_TRACKS = 12;
  public final int INFINITY= 10000000;
  private MidiIO midiIO;
  private HashMap instruments;



  public DrummerMidi(PApplet hnd)     
  {
    midiIO = MidiIO.getInstance(hnd);

    //print a list with all available devices
    midiIO.printDevices();

    instruments= new HashMap();
  }



  public void sendNote(String ip, String instrument, String velocity)
  {
    try{
      sendNote(ip, Integer.parseInt(instrument), Integer.parseInt(velocity));
    }
    catch (Exception e){
    }
  }



  public void sendNote(String input){
    // parse
    String parts[] = split(input, ';');
  
   
    if (parts.length == 3){ 
      sendNote (parts[0], parts[1], parts[2]);
    }
  }


  public void sendNote(String ip, int instrument, int velocity)
  {

    // try to get the midiOut. If not available given the ip, create a new one
    MidiOut midiOut= (MidiOut)(instruments.get(ip));
    if (midiOut == null){
      int trackNum= instruments.size();
      if (trackNum > TOT_TRACKS) return;
      midiOut = midiIO.getMidiOut(trackNum, 1);
      instruments.put(ip, midiOut);
    }


    Note note;
    if (instrument == 1){
      note = new Note(MidiInstruments.BASS1, velocity,INFINITY);
    }
    else if (instrument == 2){
      note = new Note(MidiInstruments.BASS2, velocity, INFINITY);
    }
    else if (instrument == 3){
      note = new Note(MidiInstruments.HIHAT1, velocity, INFINITY);
    }
    else if (instrument ==4){
      note = new Note(MidiInstruments.HIHAT2, velocity, INFINITY);
    }
    else if (instrument==5){
      note = new Note(MidiInstruments.SNARE1, velocity, INFINITY);
    }
    else if (instrument==6){
      note = new Note(MidiInstruments.SNARE2, velocity, INFINITY);
    }
    else if (instrument==7){
      note = new Note(MidiInstruments.CRASH1, velocity, INFINITY);
    }
    else if (instrument==8){
      note = new Note(MidiInstruments.CRASH2, velocity, INFINITY);
    }
    else if (instrument==9){
      note = new Note(MidiInstruments.TOM1, velocity, INFINITY);
    }
    else if (instrument==10){
      note = new Note(MidiInstruments.TOM2, velocity, INFINITY);
    }
    else if (instrument==11){
      note = new Note(MidiInstruments.TOM3, velocity, INFINITY);
    }
    else if (instrument==12){
      note = new Note(MidiInstruments.TOM4, velocity, INFINITY);
    }
    else{
      // SILENCE
      note = new Note(0,0,0);
    }

    midiOut.sendNote(note); 
  }



  void disconnect (Client client) {
    print("Client disconnected: "+client.ip());
    instruments.remove(client.ip());
  }

}


void disconnectEvent(Client client) {
  midi.disconnect(client);
}


